package com.txzt.gaea.upgrade.project.service.impl;

import com.txzt.gaea.business.sequence.service.ISequenceService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.BudgetStatus;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.exception.CustomException;
import com.txzt.gaea.flow.model.FlowBizStep;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.upgrade.project.domain.model.BudgetManage;
import com.txzt.gaea.upgrade.project.service.IBudgetManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;


@Service
public class BudgetManageServiceImpl extends BaseServiceImpl<BudgetManage> implements IBudgetManageService {

    private final static String SEPARATOR = "-";

    @Autowired
    private FlowService flowService;
    @Autowired
    private ISequenceService sequenceService;

    /**
     * 经费预算 - 保存/提交
     * @param budgetManage
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addBudgetManage(BudgetManage budgetManage) {
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        int year = calendar.get(Calendar.YEAR);
        Integer sequence = sequenceService.getSequence("budget_manage", "budget_no");
        String budgetNo = year + SEPARATOR + "JFYS" + SEPARATOR + String.format("%03d", sequence);
        budgetManage.setBudgetNo(budgetNo);
        budgetManage.setBudgetStatus(BudgetStatus.INIT.getCode());
        budgetManage.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = sqlManager.insertTemplate(budgetManage,true);
        Long flowId = null;
        if (budgetManage.getIsCheckSubmit()) {
            //开启流程
            flowId = flowService.startFlowUpgrade(budgetManage.getId(), "tb_budget_manage","budget_status",
                    BudgetStatus.BACK.getCode(),BudgetStatus.PASS.getCode(), BudgetStatus.REJECT.getCode(),
                    BudgetStatus.INIT.getCode(),FlowFlagEnum.PROJECT_BUDGET_MANAGE.getCode(), budgetManage.getDeptId());
        }
        //添加流程id
        if (null != flowId) {
            BudgetManage target = new BudgetManage();
            target.setId(budgetManage.getId());
            target.setFlowId(flowId);
            target.setBudgetStatus(BudgetStatus.CHECK.getCode());
            sqlManager.updateTemplateById(target);
        }
        return count;
    }



    /**
     * 经费预算 - 编辑/提交
     * @param budgetManage
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editBudgetManage(BudgetManage budgetManage) {
        if (budgetManage.getIsCheckSubmit()) {
            if (null == budgetManage.getFlowId()) {
                //开启流程
                Long flowId = flowService.startFlowUpgrade(budgetManage.getId(), "tb_budget_manage","budget_status",
                        BudgetStatus.BACK.getCode(),BudgetStatus.PASS.getCode(), BudgetStatus.REJECT.getCode(),
                        BudgetStatus.INIT.getCode(),FlowFlagEnum.PROJECT_BUDGET_MANAGE.getCode(), budgetManage.getDeptId());
                budgetManage.setFlowId(flowId);
            } else if (budgetManage.getBudgetStatus().equals(BudgetStatus.BACK.getCode())) {
                flowService.nextStepOnlyForBack(budgetManage.getFlowId());
            }
            budgetManage.setBudgetStatus(BudgetStatus.CHECK.getCode());
        }
        return sqlManager.updateTemplateById(budgetManage);
    }

    @Override
    public int revocationBudgetManage(BudgetManage budgetManage) {
        if (!budgetManage.getBudgetStatus().equals(BudgetStatus.CHECK.getCode())) {
            throw new CustomException("非审核中数据不可撤回！");
        }
        List<FlowBizStep> stepList = sqlManager.lambdaQuery(FlowBizStep.class)
                .andEq(FlowBizStep::getFlowBizId, budgetManage.getFlowId())
                .orderBy("step_no asc").select();
        if (0 == stepList.size()) {
            throw new CustomException("撤回异常！");
        }else {
            if (null == stepList.get(0).getStepStatus()) {
                BudgetManage target = new BudgetManage();
                target.setId(budgetManage.getId());
                target.setBudgetStatus(BudgetStatus.INIT.getCode());
                return sqlManager.updateTemplateById(target);
            }else {
                throw new CustomException("数据已审核不可撤回！");
            }
        }
    }


    @Override
    public List<BudgetManage> selectByIds(Long[] ids) {
        return sqlManager.lambdaQuery(BudgetManage.class).andIn("id", Arrays.asList(ids)).select();
    }

    @Override
    public Long listCheckCount(BudgetManage budgetManage) {
        List<Long> list = sqlManager.select("budgetManage.selectBudgetManageCheckCount", Long.class, budgetManage);
        return list.get(0);
    }
}
